Use case: Time base in interrupt mode ¶
|
HAL1 |
HAL2 |
|---|---|
|
Start TIMx in interrupt mode
TIMx IRQ handler
Callback function
|
Start TIMx in interrupt mode
TIMx IRQ handler
Callback function
|
Sequence diagram ¶
Code snippet ¶
|
HAL1 |
HAL2 |
|---|---|
/* Declaration of the tim handle */
TIM_HandleTypeDef htim2;
/* Ensure that all required peripherals
(TIM, GPIO, NVIC, etc.) are configured properly
before starting the timer.*/
.............
/* Start the timer TIMx in interrupt mode */
HAL_TIM_Base_Start_IT(&htim2);
/* User must call the Interrupt Handler
functions in TIMx global interrupt */
void TIM2_IRQHandler(void)
{
/* Code to execute at each interrupt */
HAL_TIM_IRQHandler(&htim2);
}
/* Call the callback function */
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
/* Code to execute at each interrupt
For example, toggle an LED */
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
}
|
/* Declaration of the tim handle */
hal_tim_handle_t htim2;
/* Ensure that all required peripherals
(TIM, GPIO, NVIC, etc.) are configured properly
before starting the timer.*/
.............
/* Start the timer TIMx in interrupt mode */
HAL_TIM_Start_IT(&htim2);
/* User must call the Interrupt Handler
functions in TIM2 global interrupt */
void TIM2_IRQHandler(void)
{
/* Code to execute at each interrupt */
HAL_TIM_IRQHandler(&htim2);
}
/* Call the callback function */
void HAL_TIM_UpdateCallback(hal_tim_handle_t *htim)
{
/* Code to execute at each interrupt
For example, toggle an LED */
HAL_GPIO_TogglePin(HAL_GPIOA, HAL_GPIO_PIN_5);
}
|
Key changes ¶
The time base start API is renamed from
HAL_TIM_Base_Start_IT()toHAL_TIM_Start_IT().The update callback is renamed from
HAL_TIM_PeriodElapsedCallback()toHAL_TIM_UpdateCallback().HAL2 provides a dedicated update IRQ handler
HAL_TIM_UPD_IRQHandler()in addition to the global handlerHAL_TIM_IRQHandler().
![@startuml
' To add a number by line
'autonumber
title "Timer as basic counter HAL 1"
' Fix order of each column
participant "User Application" as App
participant "HAL TIM" as HAL_TIM
participant "MSP" as MSP
participant "IT" as IT
group Initialization
App -[hidden]>HAL_TIM
end
group Start TIMx in IT mode
App -> HAL_TIM: <b><font color=blue>HAL_TIM_Base_Start_IT</b>()
HAL_TIM -> HAL_TIM: Enable UPDATE interrupt\nEnable TIMx counter
IT <[#red]-] : <font color=red><b>TIMx IRQ
IT -> HAL_TIM: TIMx IRQ
HAL_TIM -> HAL_TIM: HAL_TIM_IRQHandler()
HAL_TIM -> HAL_TIM: Clear flag
HAL_TIM -> App: <b><font color=green>HAL_TIM_PeriodElapsedCallback</b>()
end
@enduml](../../../../../_images/plantuml-f3fabf152c0c9c7a4ae64cb909fdf329df22df44.png)
![@startuml
' To add a number by line
'autonumber
title "Timer as basic counter HAL 2"
' Fix order of each column
participant "User Application" as App
participant "HAL TIM" as Driver
participant "IT" as IT
group Initialization and Configuration
App -[hidden]>Driver
end
group Start TIMx in IT mode
App->Driver : <b><font color=blue>HAL_TIM_Start_IT</b>()
Driver->Driver : Enable UPDATE interrupt\nEnable TIMx counter
Driver --> App : HAL_OK
IT <[#red]-] : <font color=red><b>TIMx IRQ
IT -> Driver : HAL_TIM_IRQHandler()
Driver -> Driver : Clear flag
Driver -> App: <b><font color=green>HAL_TIM_UpdateCallback</b>()
App --> Driver
Driver --> IT
end
@enduml](../../../../../_images/plantuml-1db324bb78b314b18831d11c2c8620b9a41695f2.png)